package com.jucai.pppeiyouclient.weidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.jucai.pppeiyouclient.R;

/**
 * Created by Kuky on 2017-05-07.
 */

public class CouponLayout extends RelativeLayout {
    private Paint mPaint;
    private int gap, radius;
    private int horizonCircleNo, verticalCircleNo;
    private float horizontalRemain, verticalRemain;
    private boolean byHorizontal, byVertical;

    public CouponLayout(Context context) {
        this(context, null);
        init();
    }

    public CouponLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init();
    }

    public CouponLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CouponLayout);
        gap = array.getInteger(R.styleable.CouponLayout_gap, 8);
        radius = array.getInteger(R.styleable.CouponLayout_radius, 10);
        byHorizontal = array.getBoolean(R.styleable.CouponLayout_by_horizontal, false);
        byVertical = array.getBoolean(R.styleable.CouponLayout_by_vertical, true);
        array.recycle();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setDither(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (byVertical) {
            if (verticalRemain == 0) {
                verticalRemain = (h - gap) % (2 * radius + gap);
            }
            verticalCircleNo = ((h - gap) / (2 * radius + gap));
        } else {
            byHorizontal = true;
        }

        if (byHorizontal) {
            if (horizontalRemain == 0) {
                horizontalRemain = (w - gap) % (2 * radius + gap);
            }
            horizonCircleNo = ((w - gap) / (2 * radius + gap));
        } else {
            byVertical = true;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (byVertical) {
            for (int i = 0; i < verticalCircleNo; i++) {
                float y = gap + radius + verticalRemain / 2 + ((gap + radius * 2) * i);
                canvas.drawCircle(0, y, radius, mPaint);
                canvas.drawCircle(getWidth(), y, radius, mPaint);
            }
        }

        if (byHorizontal) {
            for (int i = 0; i < horizonCircleNo; i++) {
                float x = gap + radius + horizontalRemain / 2 + ((gap + radius * 2) * i);
                canvas.drawCircle(x, 0, radius, mPaint);
                canvas.drawCircle(x, getHeight(), radius, mPaint);
            }
        }
    }
}
